package com.tanran.system;

import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * 自己封装的user32
 *
 */
public class App  {

    public static String QQ = "664870911";
    public static String NAME = "坦然";
    public static String MESSAGE = "EOMS" + new Date().getTime();

    public static void main( String[] args ) throws InterruptedException, UnsupportedEncodingException {
        System.setProperty("jna.encoding","GBK");
        int hwnd=0;
        //打开聊天窗口
        Shell32DllInterface.INSTANCE.ShellExecuteA(0
                ,"open","tencent://message/?uin="+QQ,""
                , null,1);
        //寻找句柄
        while (true){
            hwnd = User32DllInterface.INSTANCE.FindWindowA("TXGuiFoundation", NAME);
            if(hwnd>0){
                System.out.println("获取到的句柄为"+hwnd);
                break;
            }
            Thread.sleep(2000);
        }


        //置焦点
        int flag =User32DllInterface.INSTANCE.SetForegroundWindow(hwnd);
        System.out.println("是否成功置焦点"+flag);
        //保持窗口在前
        User32DllInterface.INSTANCE.SetWindowPos(hwnd,-1, 0, 0, 0, 0, 3);
        System.out.println("是否成功保持窗口在前"+flag);
        //准备投递消息
        byte[] bs = MESSAGE.getBytes("GBK");
        for (byte b : bs) {
            User32DllInterface.INSTANCE.PostMessageA(hwnd,258,b,0);
        }
        //回车键
        User32DllInterface.INSTANCE.PostMessageA(hwnd,256,13,0);
        //取消窗口置顶
        User32DllInterface.INSTANCE.SetWindowPos(hwnd, -2, 0, 0, 0, 0, 3);
        //关闭窗口
        User32DllInterface.INSTANCE.CloseWindow(hwnd);

    }



}
